<template>
  <div id="wechat">
  </div>
</template>

<script>

  export default{
    name: 'wechat',
    components: {
    },
    data () {
      return {
        appid: this.getCookie('appid'),
        enurl: encodeURIComponent(this.basedomain + '#/wechatcode'),
        openid: this.getCookie('openid') || ''
      }
    },
    created: function () {
      if (this.$route.query.wxredirectname === 'routewechatticket') {
        this.setCookie('appid', this.$route.query.appid)
        this.routeto(this.$route.query.wxredirectname)
      } else if (this.$route.query.wxredirectname === 'pathto') {
        // 是否需要外跳
        const appid = this.$route.query.appid // 门店编号
        this.setCookie('appid', appid)
        sessionStorage.setItem('pathtourl', this.$route.query.url) // 外跳的链接
        sessionStorage.setItem('wxredirectname', this.$route.query.wxredirectname)
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appid + '&redirect_uri=' + this.enurl + '&response_type=code&scope=snsapi_userinfo&state=getuser&component_appid=' + this.componentappid + '#wechat_redirect'
      } else if (this.$route.query.wxredirectname === 'appraise') {
        // 扫码评价
        const mid = this.$route.query.mid === 'null' ? '' : this.$route.query.mid // 会员编号
        const sid = this.$route.query.sid // 门店编号
        const oid = this.$route.query.oid // 订单编号
        const brandid = '1' // 品牌ID
        this.$axios.post(this.apidomain + '/api/member/getAppId', {
          brandId: brandid,
          posCardNo: mid
        })
        .then((response) => {
          this.setCookie('appid', response.data.data.appId)
          const appraiseurl = this.$route.query.wxredirectname + '?mid=' + mid + '&oid=' + oid + '&sid=' + sid + '&brandid=' + brandid
          sessionStorage.setItem('wxredirectname', appraiseurl)
          window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + response.data.data.appId + '&redirect_uri=' + this.enurl + '&response_type=code&scope=snsapi_userinfo&state=getuser&component_appid=' + this.componentappid + '#wechat_redirect'
        })
      } else {
        // 正常跳转
        if ((this.appid === this.$route.query.appid) && (this.openid.length > 0)) {
          window.location.href = this.basedomain + '/#/' + this.$route.query.wxredirectname + '?' + this.getQueryAll(window.location.href)
          // this.routeto(this.$route.query.wxredirectname)
        } else {
          this.setCookie('appid', this.$route.query.appid)
          this.appid = this.$route.query.appid
          this.delCookie('openid')
          this.delCookie('islogin')
          sessionStorage.setItem('wxredirectname', this.$route.query.wxredirectname)
          window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + this.appid + '&redirect_uri=' + this.enurl + '&response_type=code&scope=snsapi_userinfo&state=getuser&component_appid=' + this.componentappid + '#wechat_redirect'
        }
      }
    },
    methods: {
      getQueryString: function (name) {
        const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')
        const r = window.location.search.substr(1).match(reg)
        if (r != null) {
          return unescape(r[2])
        } else {
          return ''
        }
      },
      getQueryAll: function (url) {
        if (url.indexOf('?') > -1) {
          const urlarr = url.split('?')
          return urlarr[1]
        } else {
          return ''
        }
      }
    }
  }
</script>
<style>
</style>
